import Rotation from "./Rotation";
import PlayerCtrl from "./PlayerCtrl";
import SpawnPlatform from "./SpawnPlatform";
import CameraFollow from "./CameraFollow";

export default class GameEnter extends Laya.Script {
    
    Column : Laya.Sprite3D;
    PrticlePre : Laya.Sprite3D;
    scene : Laya.Scene3D;

    constructor() {
		super();
	}
    onAwake()
    {
        Laya.Scene3D.load("res/Scene/LayaScene_Main/Conventional/Main.ls",Laya.Handler.create(this,this.loadFinisde));
        Laya.stage.on("ColumnMove", this, this.ColumnMove);
        Laya.stage.on("CreatePrticle",this,this.SpawnPrticle);
        Laya.stage.on("ContinueGame",this,function()
        {
            this.Column.transform.localPositionY = 0;
        });
    }
    

    loadFinisde(scene)
    {
        this.scene = scene;
        this.scene.zOrder = -1;
        Laya.stage.addChild(scene);
        let parent = scene.getChildByName("Parent") as Laya.Sprite3D;
        parent.addComponent(Rotation).init(scene);
        let player = scene.getChildByName("Player");
        player.addComponent(PlayerCtrl);
        player.addChild(scene.getChildByName("TrailRender"));
        this.Column = parent.getChildByName("Column") as Laya.Sprite3D;
        let platform = parent.getChildByName("Platform") as Laya.Sprite3D;
        //创建预制体
        let platformPre = Laya.Sprite3D.instantiate(platform);

        (platform as Laya.Sprite3D).active = false;

        this.owner.addComponent(SpawnPlatform).init(platformPre,parent);

        scene.getChildByName("Main Camera").addComponent(CameraFollow);

        //粒子特效
        this.PrticlePre = Laya.Sprite3D.instantiate(scene.getChildByName("Prticle"));
        this.PrticlePre.active = false;
        
    }

    ColumnMove()
    {
        this.Column.transform.localPositionY -= 1.5;
    }
    SpawnPrticle(pos:Laya.Vector3)
    {
        let prticle = Laya.Pool.getItemByCreateFun("Prticle",this.CreateFun,this) as Laya.Sprite3D;
        this.scene.addChild(prticle);
        prticle.transform.position = pos;
        prticle.active = true;
        Laya.timer.once(1500,this,function () {
            prticle.removeSelf();
            Laya.Pool.recover("Prticle",prticle);
        });
    }
    CreateFun()
    {
        return Laya.Sprite3D.instantiate(this.PrticlePre,this.scene);
    }
}
